package com.mslinn.yumifier.model {
    import mx.collections.ArrayCollection;


    /** Value object for:
    * <pre>&lt;posts user="user" dt="2007-12-11" tag="">
  &lt;post href="http://www.yahoo.com/"
        hash="2f9704c729e7ed3b41647b7d0ad649fe"
      description="Yahoo!"
      extended="My favorite site ever"
      tag="yahoo web search" time="2007-12-11T00:00:07Z" others="433" />
  &lt;/posts></pre> */
    [Bindable] public class Item {
        public var description:String;
        public var extended:String;
        public var href:String;
        public var shared:Boolean;
        public var tags:String;
        public var date:Date;

        public function Item(description:String, extended:String, href:String, shared:Boolean, tags:String, date:Date) {
            this.description = description;
            this.extended = extended;
            this.href = href;
            this.shared = shared;
            this.tags = tags;
            this.date = date;
        }
        
        public function clone():Item {
            var clone:Item = new Item(description, extended, href, shared, tags, date);
            return clone;
        }
        
        /** Convert an XML representation of a list of tags to an ArrayCollection of Items. */ 
        public static function decode(xml:XML):ArrayCollection {
            var items:Array = [];
            for each (var itemXml:XML in xml..post) {
                var ts:Date = parseDate(itemXml.@time[0]);
                var item:Item = new Item(itemXml.@description, itemXml.@extended, itemXml.@href, itemXml.@shared=="yes", itemXml.@tag, ts);
                items.push(item);
            }
            return new ArrayCollection(items);
        }
        
        public static function parseDate(value:String):Date {
            var dateStr:String = value;
            dateStr = dateStr.replace(/-/g, "/");
            dateStr = dateStr.replace("T", " ");
            dateStr = dateStr.replace("Z", " GMT-0000");
            return new Date(Date.parse(dateStr));
        }
    }
}